package com.qing.common.executor

import com.qing.common.base.BaseExecutor

const val BACKGROUND_EXECUTOR_KEY = "background_executor"

const val MAIN_EXECUTOR_KEY = "main_executor"

object ExecutorFactory {
    private val mExecutors = mutableMapOf<String, BaseExecutor>()

    fun getOrCreateBackgroundExecutor(): BaseExecutor {
        if (mExecutors.containsKey(BACKGROUND_EXECUTOR_KEY)) {
            return mExecutors[BACKGROUND_EXECUTOR_KEY]!!
        }
        val executor = DefaultBackgroundExecutor()
        putExecutor(BACKGROUND_EXECUTOR_KEY, executor)
        return executor
    }

    fun getOrCreateMainExecutor(): BaseExecutor {
        if (mExecutors.containsKey(MAIN_EXECUTOR_KEY)) {
            return mExecutors[MAIN_EXECUTOR_KEY]!!
        }
        val executor = DefaultMainExecutor()
        putExecutor(MAIN_EXECUTOR_KEY, executor)
        return executor
    }

    fun putExecutor(key: String, executor: BaseExecutor) {
        mExecutors[key] = executor
    }
}